<?php

namespace App\Http\Controllers\Common;

use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Common\Department;
use App\Helpers\Code;

class DepartmentController extends Controller
{

    public function __construct()
    {
        $this->rules = [
            'title' => 'bail|required|unique:departments|max:50',
            'description' => 'max:100'
        ];

        $this->messages = [
            'title.required' => trans('user.department.title_required'),
            'title.unique' => trans('user.department.title_unique'),
            'title.max' => trans('user.department.title_max'),
            'description.max' => trans('user.department.description_max')
        ];

        $this->model = new Department();
    }

    /**
     * 添加部门
     * @param Request $request
     * @return array
     * todo:权限
     */
    public function addDepartment(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $this->result['data'] = $this->model->addDepartment($data);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除部门
     * @param $id
     * @return array
     * todo:权限
     */
    public function deleteDepartment($id)
    {
        try {
            $model = $this->model->deleteDepartment($id);

            $this->checked($model, ['null' => 'common.delete_failure', Code::INSIDE_EXIST_DEPENDENCE => 'user.department.delete_failure_dependence']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * @param $id
     * 修改部门
     * @param Request $request
     * @return array
     * todo:权限
     */
    public function editDepartment($id, Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['data_scope']));

            $this->rules['title'] = ['bail', 'required', 'max:50', \Illuminate\Validation\Rule::unique('departments')->ignore($id)];

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editDepartment($id, $data);

            $this->checked($model, ['null' => 'common.edit_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取部门列表 可搜索（title）
     * @param Request $request
     * @return array
     */
    public function getDepartmentList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int) $request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            return preprocess($this->model->getDepartmentList($limit, $sorter, $search));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取全部部门
     * @return array
     */
    public function getDepartmentListAll()
    {
        try {
            $this->result['data'] = $this->model->getDepartmentListAll();
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取部门下成员列表
     * @param $id
     * @return array
     */
    public function getDepartmentUserList($id)
    {
        try {
            $model = $this->model->getDepartmentUserList($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取单个部门信息
     * @param $id
     * @return array
     */
    public function getDepartment($id)
    {
        try {
            $model = $this->model->getDepartment($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

}
